#include <iostream>
#include <string.h>
using std::string;
using std::cout;
using std::endl;
class String{
public:
    String()
    :_pstr(new char[1])
    {

    }
    String(const char* pstr)
    :_pstr(new char [strlen(pstr)+1]())
    {
        strcpy(_pstr,pstr);
    }
    String(const String &rhs)
    :_pstr(new char [strlen(rhs._pstr)+1]())
    {
        strcpy(_pstr,rhs._pstr);
    }
    String &operator=(const String & rhs){
        if(this!=&rhs){
        delete [] _pstr;
        _pstr=new char [strlen(rhs._pstr)+1]();
        strcpy(_pstr,rhs._pstr);
        }
        return *this;
    }
    String & append(const String & rhs){
        char * temp=new char [strlen(_pstr)+strlen(rhs._pstr)+1]();
        strcpy(temp,_pstr);
        strcat(temp,rhs._pstr);
        delete [] _pstr;
        _pstr=temp;
        return *this;
    }
    String &append(const char * pstr){
        String temp(pstr);
        append(temp);
        return *this;
    }
    ~String(){
        if(_pstr){
            delete[] _pstr;
            _pstr=nullptr;
        }
    }
    void print(){
        cout<<_pstr<<endl;
    }

    size_t length()const{
        return strlen(_pstr);
    }
    const char* c_str()const{
        return _pstr;
    }
private:
    char *_pstr;

};
void test(){
    String s1("Hello");
    s1.print();

    String s2=s1;
    s2.print();

    String s3="world";
    s3.print();
    s3=s2;
    s3.print();
    cout<<s3.length()<<endl;
    
}  
void test2(){
    String s1("hello");
    String s2("world");
    s1.append(s2);
    s1.print();
}
void test3(){
    String s1="hello";
    const char *pstr2="world";
    s1.append(pstr2);
    s1.print();
}
int main(int argc, char * argv[]){
    test();
    test2();
    test3();
    return 0;
}

